package com.example.listenerrecorder;

import android.Manifest;
import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.otaliastudios.cameraview.CameraView;

import java.util.ArrayList;
import java.util.List;

import me.weyye.hipermission.HiPermission;
import me.weyye.hipermission.PermissionCallback;
import me.weyye.hipermission.PermissionItem;



public class MainActivity extends AppCompatActivity {
    //监听器状态提示文字
   // TextView ShakeStateHint,PhoneStateHint;

    TextView textView1;
    TextView textView2;

//分别是摇晃录音和电话录音类的对象
    ShakeRecord record1 = new ShakeRecord();
    PhoneRecord record2 = new PhoneRecord();
    //打开两个监听器的按钮
    Button StartShakeRecord,StartPhoneRecord;
    CameraView cameraView;

    boolean bn_state1 = true,bn_state2 = true;//两个监听按钮状态

    private void showToast(String text) {
        Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StartShakeRecord =findViewById(R.id.bn1);
        StartPhoneRecord=findViewById(R.id.bn2);

       // ShakeStateHint =  findViewById(R.id.shake);
       // PhoneStateHint=findViewById(R.id.phone);

        textView1 =  findViewById(R.id.txt1);
        textView2 =  findViewById(R.id.txt2);

        cameraView = findViewById(R.id.camera);
        //震动操作、摇晃监听器、电话监听器

        final VibratorUtil vibratorUtil = new VibratorUtil(this);
        final ShakeListener shakeListener = new ShakeListener(this);
        final PhoneListener phoneListener = new PhoneListener(this,cameraView);


//以下为申请权限操作，此处调用了'me.weyye.hipermission:library:1.0.7'这个库，用于快速申请权限并且申请界面美观
        List<PermissionItem> permissonItems = new ArrayList<PermissionItem>();
        permissonItems.add(new PermissionItem(Manifest.permission.RECORD_AUDIO, "麦克风", R.drawable.permission_ic_micro_phone));
        permissonItems.add(new PermissionItem(Manifest.permission.READ_PHONE_STATE, "电话", R.drawable.permission_ic_phone));
        permissonItems.add(new PermissionItem(Manifest.permission.WRITE_EXTERNAL_STORAGE, "存储空间", R.drawable.permission_ic_storage));
        permissonItems.add(new PermissionItem(Manifest.permission.VIBRATE, "震动", R.drawable.permission_ic_sensors));
        permissonItems.add(new PermissionItem(Manifest.permission.CAMERA, "相机", R.drawable.permission_ic_storage));

        HiPermission.create(MainActivity.this)
                .permissions(permissonItems)
                .checkMutiPermission(new PermissionCallback() {
                    @Override
                    public void onClose() {

                        showToast("用户关闭权限申请");
                    }

                    @Override
                    public void onFinish() {
                        showToast("所有权限申请完成");
                    }

                    @Override
                    public void onDeny(String permission, int position) {

                    }

                    @Override
                    public void onGuarantee(String permission, int position) {

                    }
                });





        //设置摇晃监听器
        shakeListener.setOnShakeListener(new ShakeListener.OnShakeListener() {
            @Override
            //重写了监听器的onShake方法
            public void onShake() {
                //根据对象status来决定开始录音还是结束录音
                if (!record1.status) {
                    record1.StartRecord(textView1);
                    //开始录音时震动
                    vibratorUtil.Vibrate(new long[]{100, 100, 10, 100});
                } else
                    record1.EndRecord(textView1);
                vibratorUtil.Vibrate(new long[]{100, 100, 10, 100});
            }
        });
//电话监听器，结构同上
        phoneListener.setOnPhoneListener(new PhoneListener.OnPhoneListener() {
            @Override
            //重写方法让来电时开始录音
            public void onCall() {
                if (!record2.status) {
                    record2.StartRecord(textView2);
                    vibratorUtil.Vibrate(new long[]{100, 500, 10, 500});
                } else
                    record2.EndRecord(textView2);
                vibratorUtil.Vibrate(new long[]{100, 500, 10, 500});

            }
        });



        //摇晃监听器按钮
        StartShakeRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //根据按钮状态变量决定操作
                if (bn_state1) {
                    //ShakeStateHint.setText("已启动");
                    StartShakeRecord.setText("结束摇晃监听");
                    bn_state1 = false;
                    //监听器启动
                    shakeListener.start(textView1);

                } else {
                    //ShakeStateHint.setText("未启动");
                    StartShakeRecord.setText("开始摇晃监听");
                    bn_state1 = true;
                    //如果正在进行录音，关闭监听器时也会关闭录音
                    if (record1.status) {
                        record1.EndRecord(textView1);
                        vibratorUtil.Vibrate(new long[]{100, 500, 10, 500});
                    }

                    //关闭监听
                    shakeListener.stop(textView1);

                }
            }
        });
//电话监听器按钮，结构同上
        StartPhoneRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bn_state2) {
                   // PhoneStateHint.setText("已启动");
                    StartPhoneRecord.setText("结束电话监听");
                    bn_state2 = false;
                    phoneListener.start(textView2);

                } else {
                    //PhoneStateHint.setText("未启动");
                    StartPhoneRecord.setText("开始电话监听");
                    bn_state2 = true;
                    //关闭正在进行的录音

                    if (record2.status) {
                        record2.EndRecord(textView2);
                        vibratorUtil.Vibrate(new long[]{100, 500, 10, 500});
                    }
                    //关闭监听

                    phoneListener.stop(textView2);
                }
            }
        });



    }
//下面的重写方法是相机库的必须操作
    @Override
    protected void onStart() {
        super.onStart();
        cameraView.start();
    }

    @Override
    protected void onPause() {
        cameraView.stop();
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        cameraView.destroy();
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }



}

